<template>
  <div>
    <Content style="text-align:right">
      <Form :label-width="140">
        <FormItem label="商品名称">
          <Checkbox v-for="(item,id) in seletions" :key="id" disabled :value='true'>{{item.skuName}}</Checkbox>
        </FormItem>
        <FormItem label="导航菜单">
					<Select placeholder="选择首页导航菜单" style="width:220px" clearable v-model="moduleId">
            <Option v-for="item in moduleList" :value="item.moduleId" :key="item.moduleId">{{item.moduleName}}</Option>
          </Select>
				</FormItem>
        <FormItem label="零售分润规则">
					<Select placeholder="选择分润规则" style="width:220px" clearable v-model="profitRulesId">
            <Option v-for="(item,index) in profitRuleList" :value="item.profitRulesId" :key="index">{{item.profitName}}</Option>
          </Select>
				</FormItem>
        <FormItem label="商品分组">
					<Select placeholder="选择商品分组" style="width:220px" clearable v-model="goodsGroupId">
            <Option v-for="item in goodsGroupList" :value="item.groupId" :key="item.groupId">{{item.groupName}}</Option>
          </Select>
				</FormItem>
        <template v-if="isGoods">
          <FormItem label="支持七天内退货">
            <RadioGroup v-model.number="salesReturn">
              <Radio :label="1">
                <span>是</span>
              </Radio>
              <Radio :label="0">
                <span>否</span>
              </Radio>
            </RadioGroup>
          </FormItem>
          <FormItem label="配送方式(可多选)">
            <CheckboxGroup v-model="deliveryMode">
              <Checkbox :label="0">
                <Icon type="md-cube" size="20" />
                <span>送货上门</span>
              </Checkbox>
              <Checkbox :label="1">
                <Icon type="ios-basket" size="20" />
                <span>自提/核销</span>
              </Checkbox>
            </CheckboxGroup>
          </FormItem>
          <FormItem label="是否包邮">
            <RadioGroup v-model.number="postalStatus">
              <Radio :label="1">
                <span>是</span>
              </Radio>
              <Radio :label="0">
                <span>否</span>
              </Radio>
              <Radio :label="2">
                <span>部分地区包邮</span>
              </Radio>
            </RadioGroup>
          </FormItem>
          <FormItem label="联合会员特权商品">
            <RadioGroup v-model.number="unionVipType">
              <Radio :label="0">
                <span>湘商会员特权</span>
              </Radio>
            </RadioGroup>
          </FormItem>
          <FormItem label="零售单品佣金比例" required>
            <InputNumber :disabled="canProfitRatio" class="view-input" :max="100" :min="0" :formatter="value => `${value}%`" :parser="value => value.replace('%', '')" v-model="plfmProfitRatio" placeholder="请输入零售单品佣金比例"></InputNumber>
            <span style="color:gray;font-size:12px">单品总交易额×{{plfmProfitRatio}}% = 平台佣金</span>
          </FormItem>
          <FormItem label="是否支持分销采购">
            <RadioGroup v-model.number="auditStatus">
              <Radio :label="1">
                <span>是</span>
              </Radio>
              <Radio :label="0">
                <span>否</span>
              </Radio>
            </RadioGroup>
          </FormItem>
          <FormItem v-if="auditStatus === 1" label="分销单品佣金比例">
            <InputNumber :disabled="canProfitRatio" class="view-input" :max="100" :min="0" :formatter="value => `${value}%`" :parser="value => value.replace('%', '')" v-model="scmProfitRatio" placeholder="请输入批发单品佣金比例"></InputNumber>
            <span style="color:gray;font-size:12px">分销单品总交易额×{{scmProfitRatio}}% = 平台佣金</span>
          </FormItem>
          <FormItem v-if="auditStatus === 1" label="批发起订量">
            <InputNumber class="view-input" :min="1" v-model="miniBuy" placeholder="请输入批发起订量"></InputNumber>
          </FormItem>
          <FormItem v-if="auditStatus === 1" label="批发分润规则">
            <Select placeholder="选择分润规则" style="width:220px" clearable v-model="profitRulesScmId">
              <Option v-for="(item,index) in profitRuleList" :value="item.profitRulesId" :key="index">{{item.profitName}}</Option>
            </Select>
          </FormItem>
        </template>
      </Form>
      <Button type="primary" @click="Submit">发布</Button>
    </Content>
  </div>
</template>
<script>
export default {
  name: 'ReleaseModule',
  props: {
    callbank: Function,
    seletions: Array
  },
  data() {
    return {
      isGoods: true,
      moduleId: null,
      goodsGroupId: null,
      profitRulesId: null,
      profitRulesScmId: null,
      plfmProfitRatio: 0,
      scmProfitRatio: 0,
      unionVipType: null,
      canProfitRatio: !this.$store.checkAuthCode('1921'),
      deliveryMode: [],
      salesReturn: 0,
      postalStatus: 0,
      auditStatus: 0,
      miniBuy: 1,
			moduleList: [],
			goodsGroupList: [],
      profitRuleList: [],
      profitRatio: 0
    }
  },
  watch: {
    seletions(val) {
      let seletion = val[0]
      console.log(val,"this.seletions")
      if(seletion.systemPrice > 0 && seletion.salesPrice > seletion.systemPrice) {
        const diff = seletion.salesPrice.sub(seletion.systemPrice)
        console.log(diff,'diff')
        const ratio = diff.div(seletion.salesPrice,4)
        console.log(ratio,'ratio')
        this.profitRatio = ratio.mul(100)
      }
      this.plfmProfitConfig(seletion.orgId)
    }
  },
  methods: {
    //初始化
    Init(skuId) {
      this.getAppModuleList()
      this.getGoodsGroupList()
      this.getShopGoods(skuId)
    },
    getAppModuleList() {
      new this.$rest({
        gateway: 'agroerp',
				service: 'agroerp-mall',
				url: '/v1.0/app/module/page',
        query: {
          equal: {
            appType: 0,
            moduleStatus: 1
          },
          pageIndex: 1,
          pageSize: 21
        },
				action: (res)=>{
					if(res.data.code === 20000){
						this.moduleList = res.data.data.list
					} else {
						this.$Notice.warning({
							title: '获取失败',
							desc: `失败原因：${res.data.msg}`
						})
					}
				}
			}).get()
    },
    getGoodsGroupList() {
      new this.$rest({
        gateway: 'agroerp',
				service: 'agroerp-mall',
				url: '/v1.0/goods/group/page',
        query: {
          equal: {
            appCode: 'agromall'
          },
          pageIndex: 1,
          pageSize: 21
        },
				action: (res)=>{
					if(res.data.code === 20000){
						this.goodsGroupList = res.data.data.list
					} else {
						this.$Notice.warning({
							title: '获取失败',
							desc: `失败原因：${res.data.msg}`
						})
					}
				}
			}).get()
    },
    plfmProfitConfig(orgId) {
      new this.$rest({
        gateway: 'agrobiz',
				service: 'agrobiz-uc',
				url: '/v1.0/plus/plfm/profit/config',
        query: {
          equal: orgId
        },
				action: (res)=>{
					if(res.data.code === 20000){
						this.profitRuleList = res.data.data
					} else {
						this.$Notice.warning({
							title: '获取失败',
							desc: `失败原因：${res.data.msg}`
						})
					}
				}
			}).get()
    },
    getShopGoods(skuId) {
      new this.$rest({
        gateway: 'agroerp',
				service: 'agroerp-mall',
				url: '/v1.0/agromall/shop/goods/skuid',
        query: {
          equal: skuId
        },
				action: (res)=>{
					if(res.data.code === 20000 && Object.keys(res.data.data).length > 0){
						this.moduleId = res.data.data.moduleId
            this.goodsGroupId = res.data.data.goodsGroupId
            if(res.data.data.deliveryMode === 2) {
              this.deliveryMode = [0,1]
            } else {
              this.deliveryMode.push(res.data.data.deliveryMode)
            }
            this.salesReturn = res.data.data.salesReturn
            this.postalStatus = res.data.data.postalStatus
            this.auditStatus = res.data.data.auditStatus
            this.unionVipType = res.data.data.unionVipType
            this.profitRulesId = res.data.data.profitRulesId
            this.profitRulesScmId = res.data.data.profitRulesScmId
            if(res.data.data.plfmProfitRatio) {
              this.plfmProfitRatio = res.data.data.plfmProfitRatio.mul(100)
            } else {
              this.plfmProfitRatio = this.profitRatio
            }
            if(res.data.data.scmProfitRatio) {
              this.scmProfitRatio = res.data.data.scmProfitRatio.mul(100)
            }
					} else {
            this.plfmProfitRatio = this.profitRatio
            this.moduleId = null
            this.goodsGroupId = null
            this.unionVipType = null
            this.deliveryMode = []
            this.salesReturn = 0
            this.postalStatus = 0
            this.auditStatus = 0
          }
				}
			}).get()
    },
    //批量上架商品
    Submit() {
      let shopGoodsArray = []
      this.seletions.forEach(element => {
        let spuGoods = {
          orgId: element.orgId,
          orgName: element.orgName,
          shopId: element.shopId,
          shopName: element.shopName,
          categoryId: element.categoryId,
          shopCategoryId: element.shopCategoryId,
          skuId: element.skuId,
          spuId: element.spuId,
          // mchId: this.$store.getOrg('mchId'),
          eanCode: element.eanCode,
          goodsName: element.skuName,
          skuModel: element.skuModel,
          tradePrice: element.wholesalePrice, //批发价
          retailPrice: element.aroundPrice, //市场价
          salesPrice: element.salesPrice, //零售价
          stockQty: element.inventory,
          skuUom: element.skuUom,
          goodsImage: element.skuImage,
          viewLabel: element.viewLabel,
          goodsCaption: element.industrialName, //商品描述
          salesModel: element.salesModel,
          goodsType: element.spuType,
          sortIndex: element.sortIndex,
          moduleId: this.moduleId ? this.moduleId : '0',
          goodsGroupId: this.goodsGroupId ? this.goodsGroupId : '0',
          profitRulesId: this.profitRulesId ? this.profitRulesId : '0',
          profitRulesScmId: this.profitRulesScmId ? this.profitRulesScmId : '0',
          deliveryMode: this.deliveryMode.length === 2 ? 2 : this.deliveryMode[0],
          salesReturn: this.salesReturn,
          postalStatus: this.postalStatus,
          auditStatus: this.auditStatus,
          unionVipType: this.unionVipType,
          plfmProfitRatio: this.plfmProfitRatio.div(100,4),
          scmProfitRatio: this.scmProfitRatio.div(100,4),
          miniBuy: this.miniBuy,
          salesStatus: 1, //1上架状态
        }
        shopGoodsArray.push(spuGoods)
      })
      new this.$rest({
        gateway: 'agroerp',
        service: "agroerp-mall",
        url: "/v1.0/agromall/online/goods/batch",
        data: {
          pojo: shopGoodsArray,
        },
        action: (res) => {
          if (res.data.code === 20000) {
            this.$Message.success({
              content: '上架成功',
              onClose: ()=>{
                this.callbank()
              }
            })
          } else {
            this.$Notice.warning({
							title: "操作失败",
							desc: `失败原因：${res.data.msg}`
						})
          }
        },
      }).post()
    }
  },
  mounted() {
    
  },
  created() {
    
  },
};
</script>

<style lang="less" scoped>

</style>
